class GameOverPanel extends BasePanel
{
    private listContainer = null;;
    private userInfo:any = null;
    private itemList:Array<GameOverRankItem> = [];
    public constructor(data:any,index: number = GameConfig.uiIndex.over)
    {
        super(data,index);
        this.userInfo = data.mine;
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.createChildren,this);
    }
    public createChildren()
    {   
        // 提前加载一遍
        if(GlobalData.friendDataList && GlobalData.friendDataList.length){
            this.updateDisplayInfo();
        }
        this.loadRandData();
    }
    private loadRandData()
    {
        GlobalData.getAllFriendData(this.userInfo,this.showRank.bind(this));
    }
    /**
     * 将排行榜数据展示出来
     */
    private showRank(success:boolean,list: Array<any>) {

        //防止第一次打开没数据
        if (list && list.length) {
            // 为了让当前用户处于可见的最中间，当排行榜数量大于3时只显示当前用户前一位、当前用户、当前用户后一位的数据
            this.updateDisplayInfo();
        } 
    }
    private updateDisplayInfo(){
        let dataList = GlobalData.friendDataList;
        if(dataList && dataList.length){
            dataList = this.cutThreeOrLessData(dataList);
        }
        // 清空之前的显示
        let len = this.itemList.length;
        for(let i=0;i<len;i++){
            let rankItem = this.itemList.pop();
            if(rankItem && rankItem.parent){
                rankItem.parent.removeChild(rankItem);
            } 
        }
        // 添加容器
        let listRect:any = this.data.listRect;
        if(!this.listContainer){
            this.listContainer = new egret.DisplayObjectContainer();
            this.listContainer.x = listRect.x;
            this.listContainer.y = listRect.y;
            this.listContainer.width = listRect.width;
            this.listContainer.height = listRect.height;
            this.addChild(this.listContainer);
        }
        // 添加条目数据
        let width = listRect.width/3;
        let height = listRect.height;
        for(let i=0;i<dataList.length;i++)
        {
            var dataItem = dataList[i];
            let bgColor = i%2==0?0x3195ac:0x34a3bd;
            let rankItem = new GameOverRankItem(i,dataItem,width,height,bgColor);
            rankItem.x = i * width;
            this.listContainer.addChild(rankItem);
            this.itemList.push(rankItem);
        }
    }

    /**
     * 截取包含当前用户的最多三条数据
     */
    private cutThreeOrLessData(list:Array<any>): Array<any>
    {
        if(list.length<=3){
            return list;
        }
        let mineIndex = -1;
        for(let i=0;i<list.length;i++){
            if(list[i].id == this.userInfo.id){
                mineIndex = i;
                break;
            }
        }
        if(mineIndex == -1){
            return [];
        }
        if(mineIndex==0){
            return [list[mineIndex],list[mineIndex+1],list[mineIndex+2]];
        }else if(mineIndex==list.length-1){
            return [list[mineIndex-2],list[mineIndex-1],list[mineIndex]];
        }
        return [list[mineIndex-1],list[mineIndex],list[mineIndex+1]];
    }
    
}